home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / misc / gms_dev.lha / GMSDev / Source / C / 3DObjects / Torus.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-05-07  |  4.2 KB  |  163 lines

  1. /* SAS/C: sc Torus.c opt math=standard INCDIR=INCLUDES:
  2. **
  3. ** This is a demo of a spinning torus consisting entirely of dots.
  4. ** The object is pre-calculated then rotated in real time for speed,
  5. ** although things could be faster than this.
  6. */
  7.  
  8. #include <proto/dpkernel.h>
  9. #include <math.h>
  10.  
  11. BYTE *ProgName      = "Spinning Torus";
  12. BYTE *ProgAuthor    = "Paul Manias";
  13. BYTE *ProgDate      = "January 1998";
  14. BYTE *ProgCopyright = "DreamWorld Productions (c) 1996-1998.  Freely distributable.";
  15. BYTE *ProgShort     = "3-Dimensional object demonstration.";
  16.  
  17. struct GScreen *screen;
  18. struct JoyData *jport1;
  19.  
  20. void Demo(void);
  21.  
  22. struct DotPixel { double X,Y,Z; };
  23.  
  24. #define AMTCOLOURS 32
  25.  
  26. LONG palette[AMTCOLOURS+2] = {
  27.   PALETTE_ARRAY,32,
  28.   0x000000L,0x101010L,0x171717L,0x202020L,0x272727L,0x303030L,0x373737L,0x404040L,
  29.   0x474747L,0x505050L,0x575757L,0x606060L,0x676767L,0x707070L,0x777777L,0x808080L,
  30.   0x878787L,0x909090L,0x979797L,0xa0a0a0L,0xa7a7a7L,0xb0b0b0L,0xb7b7b7L,0xc0c0c0L,
  31.   0xc7c7c7L,0xd0d0d0L,0xd7d7d7L,0xe0e0e0L,0xe0e0e0L,0xf0f0f0L,0xf7f7f7L,0xffffffL
  32. };
  33.  
  34. /***********************************************************************************/
  35.  
  36. void main(void)
  37. {
  38.   if (screen = InitTags(NULL,
  39.      TAGS_SCREEN,    NULL,
  40.        GSA_BitmapTags, NULL,
  41.        BMA_Palette,    palette,
  42.        TAGEND, NULL,
  43.      GSA_Attrib,     SCR_DBLBUFFER,
  44.      TAGEND)) {
  45.  
  46.      if (jport1 = Init(Get(ID_JOYDATA),NULL)) {
  47.         Display(screen);
  48.         Demo();
  49.  
  50.      Free(jport1);
  51.      }
  52.   Free(screen);
  53.   }
  54. }
  55.  
  56. /************************************************************************************
  57. ** Longtitude deterimines the position of the dot on the horizontal axis.
  58. ** Latitude determines the position of the dot on the vertical axis.
  59. */
  60.  
  61. #define AMTDOTS 1000       /* The amount of dots in the object. */
  62. #define MAXZ    1          /* -1 < Z < +1 */
  63.  
  64. void Demo(void)
  65. {
  66.   struct DotPixel *object;
  67.   WORD   i;
  68.   WORD   offsetx = (screen->Width/2);
  69.   WORD   offsety = (screen->Height/2);
  70.   double temp;
  71.   double angle=0;
  72.   ULONG  colour;
  73.   double Z2,X2,Y2;
  74.   LONG   scale  = 32;
  75.   UWORD  anglex = 0,
  76.          angley = 0,
  77.          anglez = 0;
  78.   double u,v;
  79.   double *sine;       /* Pointer to our sine table */
  80.   double *cosine;     /* Pointer to our cosine table */
  81.  
  82.   object = AllocMemBlock(sizeof(struct DotPixel)*AMTDOTS,MEM_DATA);
  83.   sine   = AllocMemBlock(sizeof(double)*360,MEM_DATA);
  84.   cosine = AllocMemBlock(sizeof(double)*360,MEM_DATA);
  85.  
  86.   /* First calculate the X, Y and Z coordinates of our object */
  87.  
  88.   for (i=0; i<AMTDOTS; i++) {
  89.     u = ((double)FastRandom(6283))/1000;
  90.     v = ((double)FastRandom(6283))/1000;
  91.     object[i].X  = cos(u)*(2+cos(v));
  92.     object[i].Y  = sin(u)*(2+sin(v));
  93.     object[i].Z  = sin(v);
  94.   }
  95.  
  96.   /* Now generate our cosine and sinus tables */
  97.  
  98.   for (i=0; i<360; i++) {
  99.     cosine[i] = cos(angle);
  100.     sine[i]   = sin(angle);
  101.     angle    += 0.25;
  102.   }
  103.  
  104.   /* Go into our main loop */
  105.  
  106.   do
  107.   {
  108.     Query(jport1);
  109.     scale += jport1->YChange;
  110.     if (scale < 1) scale = 1;
  111.     if (scale > 100) scale = 100;
  112.  
  113.     Clear(screen->Bitmap);
  114.  
  115.     for (i=0; i<AMTDOTS; i++) {
  116.  
  117.       X2 = object[i].X;
  118.       Y2 = object[i].Y;
  119.       Z2 = object[i].Z;
  120.  
  121.       /* Rotate the X axis */
  122.  
  123.       temp = Z2;
  124.       Z2 = Z2 * cosine[anglex] - Y2   * sine[anglex];
  125.       Y2 = Y2 * cosine[anglex] + temp * sine[anglex];
  126.  
  127.       /* Rotate the Y axis */
  128.  
  129.       temp = Z2;
  130.       Z2 = Z2 * cosine[angley] - X2   * sine[angley];
  131.       X2 = X2 * cosine[angley] + temp * sine[angley];
  132.  
  133.       /* Rotate the Z axis */
  134.  
  135. //      temp = X2;
  136. //      X2 = X2 * cosine[anglez] - Y2   * sine[anglez];
  137. //      Y2 = Y2 * cosine[anglez] + temp * sine[anglez];
  138.  
  139.       /* Calculate colour based on Z position (-1 < Z < +1) */
  140.  
  141.       colour = (((Z2+MAXZ)/MAXZ) * screen->Bitmap->AmtColours)/2;
  142.  
  143.       /* Finally scale the (x,y) coordinates to enlarge or shrink the object */
  144.  
  145.       X2 *= scale;
  146.       Y2 *= scale;
  147.  
  148.       DrawPixel(screen->Bitmap,(WORD)X2+offsetx,(WORD)Y2+offsety,colour);
  149.     }
  150.     anglex++; if (anglex >= 360) anglex = 0;
  151.     angley++; if (angley >= 360) angley = 0;
  152.     anglez++; if (anglez >= 360) anglez = 0;
  153.  
  154.     WaitAVBL();
  155.     SwapBuffers(screen);
  156.   } while(!(jport1->Buttons & JD_LMB));
  157.  
  158.   FreeMemBlock(object);
  159.   FreeMemBlock(sine);
  160.   FreeMemBlock(cosine);
  161. }
  162.  
  163.